package com.haibo.result;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
 * @Author : 刘海波
 * @Date: 2024/12/25 10:36
 * @Description:
 */
@Data
public class R<T> {
    /**
     * 0：成功
     * -1：失败
     */
    @JsonProperty("hasError")
    private String hasError = null;
    @JsonProperty("errorMessage")
    private String errorMessage = null;
    private T data;
    private Integer code;
    private String message;


    public static <T> R<T> success() {
        R<T> r = new R<>();
        r.setHasError("0");
        r.setErrorMessage("");
        return r;
    }

    public static <T> R<T> success(Integer code,String message) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMessage("成功");
        return r;
    }

    public static <T> R<T> success(Integer code,String message,T data) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

    public static <T> R<T> success(T data) {
        R<T> r = new R<>();
        r.setHasError("0");
        r.setErrorMessage("");
        r.setData(data);
        return r;
    }

    public static <T> R<T> fail(String message) {
        R<T> r = new R<>();
        r.setHasError("-1");
        r.setErrorMessage(message);
        return r;
    }

    public static <T> R<T> fail(String message,T data) {
        R<T> r = new R<>();
        r.setHasError("-1");
        r.setErrorMessage(message);
        r.setData(data);
        return r;
    }

    public static <T> R<T> fail(Integer code,String message) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMessage(message);

        return r;
    }

    public static <T> R<T> fail(Integer code,String message,T data) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);

        return r;
    }

    public R<T> data(T entity) {
        this.setData(entity);
        return this;
    }

}
